package com.prs.crm.service.oa.impl;

import java.util.Collection;

import com.prs.crm.context.AppContext;
import com.prs.crm.dao.oa.MailBoxDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.MailBox;
import com.prs.crm.service.impl.BaseServiceImpl;
import com.prs.crm.service.oa.MailBoxService;

public class MailBoxServiceImpl extends BaseServiceImpl<MailBox,MailBoxDao> implements MailBoxService{

	@Override 
	public void save(MailBox box)
	{
		MailBox defaultBox = this.findDefaultMailBox(AppContext.getAccountContext().currentUser());
		if (box.getDefaultBox()!=null && box.getDefaultBox().booleanValue())
		{
			if (defaultBox!=null)
			{
				if (box.getId()==null || !box.getId().equals(defaultBox.getId()))
				{
					defaultBox.setDefaultBox(false);
					super.save(defaultBox);
				}
			}
		}
		if (defaultBox==null)
			box.setDefaultBox(true);
		super.save(box);
	}
	
	public MailBox findByAddr(Person owner,String addr)
	{
		return this.getDao().findByAddr(owner, addr);
	}
	
	public Collection<MailBox> findBoxes(Person owner)
	{
		return this.getDao().findBoxes(owner);
	}
	
	public Collection<MailBox> getAllBox()
	{
		return this.getDao().getAllBox();
	}
	
	public MailBox findDefaultMailBox(Person owner)
	{
		return this.getDao().findDefaultMailBox(owner);
	}
}
